package net.araim.bbingo.dialogs;

import net.araim.bbingo.R;
import net.araim.bbingo.interfaces.IDialogOptionSelectedListener;
import android.app.AlertDialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;

public class MainMenuDialog extends AlertDialog {

	private IDialogOptionSelectedListener optionSelectedListener = null;

	public MainMenuDialog(Context context) {
		super(context);
		View v = LayoutInflater.from(context).inflate(R.layout.mainmenu, null);
		this.setView(v);
		MenuButtonClickListener mbcl = new MenuButtonClickListener();
		v.findViewById(R.id.MenuStartButton).setOnClickListener(mbcl);
		v.findViewById(R.id.MenuOptionsButton).setOnClickListener(mbcl);
		v.findViewById(R.id.MenuShameButton).setOnClickListener(mbcl);
		v.findViewById(R.id.MenuHelpButton).setOnClickListener(mbcl);
		v.findViewById(R.id.MenuExitButton).setOnClickListener(mbcl);
	}

	public void setOnOptionSelectedListener(IDialogOptionSelectedListener lsnr) {
		this.optionSelectedListener = lsnr;
	}

	private class MenuButtonClickListener implements android.view.View.OnClickListener {

		public void onClick(View v) {
			if (optionSelectedListener != null) {
				optionSelectedListener.onOptionSelected(MainMenuDialog.this, v.getId());
			}
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if(getOwnerActivity() != null){
				getOwnerActivity().finish();
			}
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

}
